package org.fas.container
{
	import flash.display.DisplayObject;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	
	import org.foxas.DataObject.DaPos;
	import org.foxas.base.FFunc;
	import org.foxas.base.FMath;
	public class Drager extends Sprite{
		//now
		protected var mx0:Number = 0;//mouseX when move now
		protected var my0:Number = 0;
		//last
		protected var mx1:Number = 0;//mouseX when move last
		protected var my1:Number = 0;
		//
		protected var speedX:Number = 0;
		protected var speedY:Number = 0;
		//
		protected var par_r:Object;//this.parent
		public var w:Number = 0;
		public var h:Number = 0;
		public var oriW:Number = 0;
		public var oriH:Number = 0;
		public function setOriWH(_w:Number,_h:Number):void{
			oriW = _w;
			oriH = _h;
			w=_w;
			h=_h;
		}
		protected var isDown_r:Boolean = false;
		public function Drager(){init();}
		protected function init():void{
			this.addEventListener(Event.ADDED_TO_STAGE,addStageHandle);
		}
		public function addStageHandle(_e:Event):void{
			par = this.parent;
			this.addEventListener(MouseEvent.MOUSE_DOWN,mouseDownHandle);
			//this.addEventListener(MouseEvent.ROLL_OVER,mouseOverHandle);
			//this.addEventListener(MouseEvent.ROLL_OUT,mouseOutThisHandle);	
		}
		public function set par(_ob:Object):void{
			par_r=_ob;
			par_r.addEventListener(MouseEvent.MOUSE_UP,mouseUpHandle);
			par_r.addEventListener(MouseEvent.MOUSE_OUT,mouseOutHandle);
		}
		public function get par():Object{
			return par_r;
		}
		private var child:Object;
		public function append(_ob:DisplayObject):void{
			child = _ob;
			this.addChild(child as DisplayObject);
			this.setOriWH(child.width,child.height);
		}
		//handle
		protected function clickHandle(_e:MouseEvent=null):void{
		}
		public function mouseDownHandle(_e:MouseEvent=null):void{
			this.isDown = true;
			mx0 = par.mouseX;
			my0 = par.mouseY;
		}
		public function mouseUpThisHandle(_e:MouseEvent=null):void{
			mouseUpHandle(_e);
		}
		public function mouseUpHandle(_e:MouseEvent=null):void{
			if(isDown){
				this.isDown = false;
			}
		}
		protected function mouseOverHandle(_e:MouseEvent):void{
			if(!isDown){}
		}
		protected function mouseOutThisHandle(_e:MouseEvent):void{
		}
		protected function mouseOutHandle(_e:MouseEvent=null):void{//only for parent
		}
		protected function set isDown(_bl:Boolean):void{
			isDown_r = _bl;
			if(_bl){//true
				this.addEventListener(Event.ENTER_FRAME,frameHandle);
			}else{//false
				this.removeEventListener(Event.ENTER_FRAME,frameHandle);
			}
		}
		protected function get isDown():Boolean{
			return isDown_r;
		}
		protected function frameHandle(_e:Event):void{
			dragCount(par.mouseX,par.mouseY);
		}
		public function dragCount(_mouseX:Number,_mouseY:Number):void{
			mx1 = _mouseX;
			my1 = _mouseY;
			//
			var cx0:Number;
			var cy0:Number;
			var _r0:Number;
			var _r1:Number;
			var _dis:Number;
			var cx:Number=this.x;
			var cy:Number=this.y;
			//core code of Virtual Reality
				cx0 = this.centerX;
				cy0 = this.centerY;
				_r0 = FMath.angle((mx0-cx0),(my0-cy0));
				_r1 = FMath.angle((mx1-cx0),(my1-cy0));
				_dis = FMath.dis((mx0-cx0),(my0-cy0));
				this.rotation += (_r1-_r0);
				this.centerX = mx1 - _dis*FMath.cos(_r1);
				this.centerY = my1 - _dis*FMath.sin(_r1);
			
			//end
			speedX = (speedX + mx1 - mx0)/2;//speed of mouse x offset
			speedY = (speedY + my1 - my0)/2;
				var _x:Number=0; 
				var _y:Number=0;
				_x += speedX*FMath.cos(this.rotation);
				_y -= speedX*FMath.sin(this.rotation);
				_x += speedY*FMath.sin(this.rotation);//offset x of mouse relative to displayOb
				_y += speedY*FMath.cos(this.rotation);
				var thisSpeedX:Number;
				var thisSpeedY:Number;
				thisSpeedX = _x;
				thisSpeedY = _y;
				var _daPos:DaPos = FFunc.posParToChild(this,mx0,my0);
//				child.buildAll(thisSpeedX,thisSpeedY,this.mouseX,this.mouseY,this.x-cx,this.y-cy);
				child.buildAll(thisSpeedX,thisSpeedY,this.mouseX,this.mouseY,mx1-mx0,my1-my0,_r1-_r0);
				//point this.mouseX this.mouseY
				//speed selfSpeedX selfSpeedY
			mx0 = mx1;
			my0 = my1;
		}
		public function set scale(_v:Number):void{
			var _cx:Number = this.centerX;
			var _cy:Number = this.centerY;
			this.scaleX = _v;
			this.scaleY = _v;
			this.w = this.oriW*this.scaleX;
			this.h = this.oriH*this.scaleY;
			this.centerX = _cx;
			this.centerY = _cy;
		}
		public function get scale():Number{
			return this.scaleX;
		}
		/**
		 * get center position(x) across value of origin position(x)
		 */
		public function get centerX():Number{
			var _w:Number = this.w;
			var _h:Number = this.h;
			var _dis:Number = FMath.dis(_w,_h)/2;
			var _cr:Number = FMath.angle(_w,_h);
			return this.x + _dis*FMath.cos(this.rotation+_cr);
		}
		/**
		 * set origin position(x) across value of center position(x)
		 */
		public function set centerX(_v:Number):void{
			var _w:Number = this.w;
			var _h:Number = this.h;
			var _dis:Number = FMath.dis(_w,_h)/2;
			var _cr:Number = FMath.angle(_w,_h);
			this.x = _v - _dis*FMath.cos(this.rotation+_cr);
		}
		/**
		 * get center position(yY) across value of origin position(y)
		 */
		public function get centerY():Number{
			var _w:Number = this.w;
			var _h:Number = this.h;
			var _dis:Number = FMath.dis(_w,_h)/2;
			var _cr:Number = FMath.angle(_w,_h);
			return this.y + _dis*FMath.sin(this.rotation+_cr);
		}
		/**
		 * set origin position(y) across value of center position(y)
		 */
		public function set centerY(_v:Number):void{
			var _w:Number = this.w;
			var _h:Number = this.h;
			var _dis:Number = FMath.dis(_w,_h)/2;
			var _cr:Number = FMath.angle(_w,_h);
			this.y = _v - _dis*FMath.sin(this.rotation+_cr);
		}
	}
}